package com.cms.server.resolver.plugins;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

public class CMSPSHTFormManagerToDepartmentConfirm extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFDataSet                      BMDataset = EFDataSet.getInstance(new String[]{"F_BMBH"});
		EFDataSet                    flowDataSet = EFDataSet.getInstance("FLOW_TASK_LIST");
		EFDataSet                   flowDataSet1 = EFDataSet.getInstance("FLOW_TASK_LIST");
		EFDataSet                   flowDataSet2 = EFDataSet.getInstance("FLOW_TASK_LIST");
		EFRowSet                        BMRowSet = null;
		EFRowSet                      itemRowSet;
		EFRowSet                      flowRowSet;
		EFDataSet                    itemDataSet;
		String                            strSql = "";
		ResultSet                             rs = null;
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = "";
		String                         NODE_NAME = "";
		java.util.List<String>    dataSetKeyList = new java.util.ArrayList<String>();
		Map<String, EFRowSet>               map1 = new HashMap<String, EFRowSet>();
		Map<String, EFRowSet>               map2 = new HashMap<String, EFRowSet>();
		long                             curTime = new Date().getTime();

		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_NEXT, "N4");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "submit");
		
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			   + " FLOW_ID = 'PSHT_EDIT_FLOW' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "'"
			   + " order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet = DataSetUtils.resultSet2DataSet(rs, flowDataSet);
		
		flowRowSet = flowDataSet.getRowSet(0);

		//如果不是开始节点，则需要更新上一个流程节点任务信息，同时还要插入一条新的记录
		FLOW_ID = flowRowSet.getString("FLOW_ID", "");

		//获取提交到节点名称
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE_NEXT, "") + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) NODE_NAME = rs.getString("NODE_NAME");
			
		//查询部门
		strSql = " select F_BMBH, F_BMMC from HYBMZD where F_SYZT = '1'";
		rs = stmt.executeQuery(strSql);
		BMDataset = DataSetUtils.resultSet2DataSet(rs, BMDataset);

		//等待部门确认的流程任务
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	           + " FLOW_ID = 'PSHT_EDIT_FLOW' and TASK_STATUS = 'ending' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet1 = DataSetUtils.resultSet2DataSet(rs, flowDataSet1);
		map1 = convertDataSetToMap(flowDataSet1);
		
		//已经部门确认的流程任务
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	           + " FLOW_ID = 'PSHT_EDIT_FLOW' and RESR_STATUS = 'pending' and RESR_IN_CAUSE = 'submit' and NODE_TAG = 'N4' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet2 = DataSetUtils.resultSet2DataSet(rs, flowDataSet2);
		map2 = convertDataSetToMap(flowDataSet2);
		
		//更新上一个任务节点的状态
		strSql = "update FLOW_TASK_LIST set RESR_STATUS = 'processed', RESR_OUT_CAUSE = '" 
			   + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "") + "' where OP_ID = '" 
			   + flowRowSet.getString("OP_ID", "") + "' and FLOW_ID = '" + FLOW_ID + "'";
		stmt.execute(strSql);
		//操作节点
		PO.SetValueByEnvName("NODE_OP_ID", flowRowSet.getString("OP_ID", ""));

		if(billDataSet.getRowCount() > 0) {
			itemRowSet = billDataSet.getRowSet(0);
			dataSetKeyList = itemRowSet.getDataSetKeyList();
			if(dataSetKeyList.size() > 0) {
				itemDataSet = itemRowSet.getDataSet(dataSetKeyList.get(0));
				for(int i = 0; i < itemDataSet.getRowCount(); i++){
			    	itemRowSet = itemDataSet.getRowSet(i);
			    	//如果多个部门同时提交，时间可能一样，所以为了防止插入引起主键重复，所以加1
				    BMRowSet = (EFRowSet) BMDataset.getRowSet(itemRowSet.getString("F_BMBH", ""));		
				    if(map1.get(itemRowSet.getString("F_BMBH", "")) == null && map2.get(itemRowSet.getString("F_BMBH", "")) == null) {
					    insertNewFlowRow(stmt, PO, F_DJBH, F_GUID, F_KJQJ, NODE_NAME, BMRowSet, flowRowSet, curTime);
					    curTime++;
				    }
				}
			}
		}
	}
	
	
	private Map<String, EFRowSet> convertDataSetToMap(EFDataSet dataset) {
		Map<String, EFRowSet> map = new HashMap<String, EFRowSet>();
		
		for(int i = 0; i < dataset.getRowCount(); i++) {
			map.put(dataset.getRowSet(i).getString("TASK_TO_UNIT", ""), dataset.getRowSet(i));
		}
		return map;
	}
	
	private void insertNewFlowRow(JStatement stmt, JParamObject PO, String F_DJBH, String F_GUID, String F_KJQJ, String NODE_NAME, EFRowSet BMRowSet, EFRowSet flowRowSet, long curTime) throws Exception {
		String                            strSql = "";
		ResultSet                             rs = null;
		EFRowSet                      formItemRS = EFRowSet.getInstance();
		
		strSql = "select * from PSHTMX where F_KJND = '" + F_KJQJ + "' and F_DJBH = '" + F_DJBH + "' and F_GUID = '" + F_GUID + "' and F_BMBH = '" + BMRowSet.getString("F_BMBH", "") + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) formItemRS = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, formItemRS);
		
		//每一个部门一条任务信息
	    strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
			   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS," 
			   + "RESR_IN_CAUSE,RESR_OUT_CAUSE,F_NUM01,F_NUM02," ;
	    for(int j = 1; j <= 20; j++) {
	    	if(j < 10) {
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
	    			strSql += "F_CHAR0" + j + ",";
	    		}
    		} else {
    			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
    				strSql += "F_CHAR" + j + ",";
	    		}
    		}
    	}
	    
	    if(formItemRS.getDate("F_QSRQ", null) != null) {
	    	strSql += "F_DATE01,";
	    }
	    if(formItemRS.getDate("F_JZRQ", null) != null) {
	    	strSql += "F_DATE02,";
	    }
	    if(formItemRS.getString("XG_JZRQ", null) != null) {
	    	strSql += "F_DATE03,";
	    }
	    strSql += "TASK_TO_UNIT,TASK_TO_UNIT_NAME) values ('"
	            + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
			    + F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
			    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
				+ curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NOTE, "") + "', 'processing', '"
				+ PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '"
				+ PO.GetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE_NEXT, "") + "', '" + NODE_NAME 
				+ "', '" + flowRowSet.getString("OP_ID", "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
			    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '',"
			    + formItemRS.getNumber("F_WCTS", 0.0).intValue() + "," + formItemRS.getNumber("XG_WCTS", 0.0).intValue() + ",";
	    for(int j = 1; j <= 20; j++) {
	    	if(j < 10) {
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
	    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
	    		}
    		} else {
    			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
	    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null)  + "',";
	    		}
    		}
    	}
	    if(formItemRS.getDate("F_QSRQ", null) != null) {
	    	strSql += "'" + formItemRS.getDate("F_QSRQ", null).toString() + "', ";
	    }
	    if(formItemRS.getDate("F_JZRQ", null) != null) {
	    	strSql += "'" + formItemRS.getDate("F_JZRQ", null).toString() + "', ";
	    }
	    if(formItemRS.getString("XG_JZRQ", null) != null) {
	    	strSql += "'" + formItemRS.getString("XG_JZRQ", null).toString() + "', ";
	    }
	    strSql += "'" + BMRowSet.getString("F_BMBH", "") + "', '" + BMRowSet.getString("F_BMMC", "") + "')";
		stmt.execute(strSql);
	}
}
